package com.example.activity.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.activity.mapper.DeployInfoMapper;
import com.example.activity.mapper.FileInfoMapper;
import com.example.activity.po.DeployInfoPO;
import com.example.activity.po.FileInfoRelPO;
import com.example.activity.service.DeployInfoService;
import com.example.activity.service.FileInfoRelService;
import com.example.activity.so.acvitivi.DeployInfoSO;
import com.example.activity.vo.PageResultVO;
import com.example.activity.vo.acvitivi.DeployInfoVO;
import com.example.activity.vo.upload.FileUpDowmInfoVO;
import com.github.pagehelper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeployInfoServiceImpl extends ServiceImpl<DeployInfoMapper, DeployInfoPO> implements DeployInfoService {

    @Resource
    private DeployInfoMapper deployInfoMapper;

    @Resource
    private FileInfoMapper fileInfoMapper;

    @Resource
    private FileInfoRelService fileInfoRelService;

    @Override
    public PageResultVO<DeployInfoVO> deployInfoPageList(DeployInfoSO deployInfoSO) {
        if (deployInfoSO == null) {
            return null;
        }

        PageHelper.startPage(deployInfoSO.getPageNum(), deployInfoSO.getPageSize());
        List<DeployInfoVO> deployedVOList = deployInfoMapper.deployInfoList(deployInfoSO);
        if (CollectionUtil.isEmpty(deployedVOList)) {
            return null;
        }
        PageInfo<DeployInfoVO> pageInfo = new PageInfo<>(deployedVOList);

        // 封装文件信息
        List<Long> deployedIdList = deployedVOList.stream().map(DeployInfoVO::getId).collect(Collectors.toList());
        List<FileInfoRelPO> fileInfoRelPOList = fileInfoRelService.lambdaQuery().in(FileInfoRelPO::getTableIdStr, deployedIdList).list();
        List<Long> fileIdList = fileInfoRelPOList.stream().map(FileInfoRelPO::getFileInfoId).collect(Collectors.toList());
        FileUpDowmInfoVO fileInfo = new FileUpDowmInfoVO();
        fileInfo.setIdList(fileIdList);
        List<FileUpDowmInfoVO> fileUpDowmInfoVOList = fileInfoMapper.fileRelInfoList(fileInfo);

        // 文件id-文件信息 map
        Map<Long, FileUpDowmInfoVO> fileId_fileRelInfo_map = fileUpDowmInfoVOList.stream().collect(Collectors.toMap(FileUpDowmInfoVO::getId, o -> o));
        // 业务主键_文件id map
        Map<String, Long> tableIdStr_fileId_map = fileInfoRelPOList.stream().collect(Collectors.toMap(FileInfoRelPO::getTableIdStr, FileInfoRelPO::getFileInfoId));

        for (DeployInfoVO deployInfoVO : deployedVOList) {
            // 业务主键
            Long id = deployInfoVO.getId();
            // 文件id
            Long fileId = tableIdStr_fileId_map.get(String.valueOf(id));
            FileUpDowmInfoVO fileUpDowmInfoVO = fileId_fileRelInfo_map.get(fileId);
            deployInfoVO.setFileUpDowmInfoVO(fileUpDowmInfoVO);
        }



        return (PageResultVO<DeployInfoVO>) new PageResultVO(pageInfo, deployedVOList);
    }



}
